package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsOwnerOrdertype;
import com.jy.wms.service.WmsOwnerOrdertypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WmsOwnerOrdertyperController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author Jane
 * @Date Tue Aug 29 15:42:49 CST 2017
 */
@Controller
@RequestMapping("/wmsOwnerOrderType")
@Api(value = "/wmsOwnerOrderType", description = "关于货主订单类型操作")
public class WmsOwnerOrdertyperController extends BaseController<WmsOwnerOrdertypeService,WmsOwnerOrdertype> {

    @PostMapping(value="/list")
    @ApiOperation(value = "货主供应商管理 POST请求查询货主供应商信息",response = JSONObject.class, notes = "POST请求查询货主供应商信息")
    public@ResponseBody JSONObject getWmsOwnerOrdertype(@RequestBody WmsOwnerOrdertype entity, HttpSession session) {
        Page<WmsOwnerOrdertype> list = new Page<WmsOwnerOrdertype>();
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                entity.setSshzid(hz.get(0).getHzid());
            }
             entity.setCkid(userinfo.getUser().getOrgId().get(0).toString());
            list= baseService.baseSelectPage(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

//    @PostMapping(value = "/delete")
//    @ApiOperation(value = "货主订单类型 多个或单个货主订单类型",response = JSONObject.class, notes = "POST删除多个或单个货主订单类型")
//    public @ResponseBody
//    JSONObject deleteWmsOwnerOrdertype(@ApiParam(required = true, name = "setModel", value = "配置参数映射对象") @RequestBody SetAndModifyModel setModel) {
//        try {
//            Integer flag=baseService.baseDeleteBathPrimaryKeys(setModel.getIds());
//            return SetStatus.setReturn(flag,true,1,"货主订单类型","删除货主订单类型","");
//        } catch (Exception e) {
//            e.printStackTrace();
//
//            return SetStatus.setReturn(0,false,1,"货主订单类型","删除货主订单类型","");
//        }
//    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "货主订单类型 新增货主订单类型",response = JSONObject.class, notes = "POST新增货主订单类型")
    public @ResponseBody JSONObject addWmsOwnerOrdertype(@RequestBody WmsOwnerOrdertype entity, HttpSession session) {
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                entity.setSshzid(hz.get(0).getHzid());
            }
            Integer lx=baseService.duplicateByDdlx(entity.getDdlx(),"",hz.get(0).getHzid(),userinfo.getUser().getOrgId().get(0).toString());
            if(lx==0){
                entity.setCkid(userinfo.getUser().getOrgId().get(0).toString());
                if("1".equals(entity.getFl())){
                    entity.setSfzj("0");
                }
                entity.setId(UUIDUtils.uuid());
                entity.setZdsh("0");
                entity.setCjsn("0");
                baseService.baseInsert(entity);
                return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"新增货主订单类型成功","",JSONObject.fromObject(entity).toString());
            }else{
                return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"货主订单类型已经存在","","");
            }
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"新增货主订单类型失败", e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"新增货主订单类型失败", e.getMessage(),"");
        }

    }

    @GetMapping(value = "/getWmsOwnerOrdertype1/{id}")
    @ApiOperation(value = "货主订单类型 查 看单个货主订单类型",response = JSONObject.class, notes = "GET查看单个货主订单类型")
    public @ResponseBody JSONObject getWmsOwnerOrdertypeById1(@PathVariable("id") String id) {
        JSONObject json = new JSONObject();
        WmsOwnerOrdertype entity= null;
        try {
            entity = baseService.baseSelectByPrimaryKey(id);
            json = JSONObject.fromObject(entity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;

    }

    @PostMapping(value = "getWmsOwnerOrdertype")
    @ApiOperation(value = "货主订单类型 查 看单个货主订单类型",response = JSONObject.class, notes = "GET查看单个货主订单类型")
    public @ResponseBody JSONObject getWmsOwnerOrdertypeById(@RequestBody SetAndModifyModel setAndModifyModel) {
        JSONObject json = new JSONObject();
        WmsOwnerOrdertype entity= null;
        try {
            entity = baseService.baseSelectByPrimaryKey(setAndModifyModel.getId());
            json = JSONObject.fromObject(entity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;

    }

    @PostMapping(value = "/edit")
    @ApiOperation(value = "货主订单类型 修改货主订单类型",response = JSONObject.class, notes = "POST修改货主订单类型")
    public @ResponseBody
    JSONObject editWmsOwnerOrdertype(@RequestBody WmsOwnerOrdertype entity, HttpSession session) {
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
            String owner=null;//货主id
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                owner=hz.get(0).getHzid();
            }
            Integer lx=baseService.duplicateByDdlx(entity.getDdlx(),entity.getId(),owner,ckid);
            if(lx==0){
                baseService.baseUpdateByPrimaryKeySelective(entity);
                return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"修改货主订单类型成功","",JSONObject.fromObject(entity).toString());
            }else{
                return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"货主订单类型已经存在","","");
            }
        }catch (ServiceException e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"修改货主订单类型失败", e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"修改货主订单类型失败", e.getMessage(),"");
        }
    }
    @GetMapping(value = "ddlxDuplicate/{ddlx}")
    @ApiOperation(value = "货主订单类型 订单类型去重",response = JSONObject.class, notes = "订单类型去重")
    public @ResponseBody JSONObject ddlxDuplicate(@PathVariable("ddlx") String ddlx, HttpSession session) {
        //获取当前仓库
        UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
        //获取当前货主
        String owner=null;//货主id
        List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
        if(hz.size()>0){
            owner=hz.get(0).getHzid();
        }
        try {
            if(ddlx==""||ddlx==null){
                ddlx="";
            }
            Integer lx=baseService.duplicateByDdlx(ddlx,"",owner,ckid);

            if(lx<=0){
                return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"订单类型无重复，可用","","");
            }else{
                return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"货主订单类型已经存在","","");
            }

        } catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"货主订单类型已经存在","","");

        }
    }

    //获取订单分类
    /*
     * select2 获取 ASN类型
     * */
    @ApiOperation(value = "select2获取订单类型", response = JSONObject.class, notes = " 订单类型")
    @PostMapping(value = "getSelectLx")
    public @ResponseBody
    JSONArray getSelectLx(HttpSession session) {
        List<WmsOwnerOrdertype> list = null;
        List<Map> tlist = new ArrayList<>();
        try {
            //用户信息
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
            List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid = null;
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
                list = baseService.getPoLx(hzid, ckid);
            }
            for (int i = 0; i < list.size(); i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", list.get(i).getId());
                map.put("mc", list.get(i).getDdlx());
                tlist.add(map);
            }


        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONArray.fromObject(tlist);
    }

    @PostMapping(value = "/getShzt")
    @ApiOperation(value = "获取审核状态", response = JSONObject.class, notes = "查询审核状态")
    public @ResponseBody
    String getShzt(String id){

        return baseService.getByShzt(id);
    }
}
